package com.cneiu.ppms.web.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.DepartmentModel;
import com.jfinal.plugin.activerecord.Page;
/**
 * 部门业务
 * @author zhaokf
 * @date 2013-04-15
 */
public class DepartmentService extends BaseService{
	
	/**
	 * 将机构数据转化成Tree格式(递归实现)
	 * @param depts
	 * @return
	 */
	public static List<HashMap<Object,Object>> treeComboBox(DepartmentModel model){
		List<HashMap<Object,Object>> smap = new ArrayList<HashMap<Object,Object>>();
		/**查询本节点下面的子节点**/
		String query_sql = "SELECT * FROM SA_DEPT WHERE DEPT_PARENT=?";
		List<DepartmentModel> depts = DepartmentModel.dao.find(query_sql,model.get("DEPT_NO"));
		/**组装本节点数据**/
		HashMap<Object, Object> map = new HashMap<Object, Object>();
		map.put("dept_no", model.get("DEPT_NO"));
		map.put("dept_name", model.get("DEPT_NAME"));
		map.put("treeText", model.get("DEPT_NO")+" "+model.get("DEPT_NAME"));
		smap.add(map);
		/**判断是否存在子节点**/
		if(depts!=null && depts.size()>0){
			HashMap<Object, Object> map1 =  null;
			/**循环子节点**/
			for(DepartmentModel dept:depts){
					map1 = new HashMap<Object, Object>();
					map1.put("dept_no", dept.get("DEPT_NO"));
					map1.put("dept_name", dept.get("DEPT_NAME"));
					map1.put("treeText", dept.get("DEPT_NO")+" "+dept.get("DEPT_NAME"));
					smap.add(map1);
					/***递归查找*/
					treeComboBox(dept);
			}
		}
		return smap;
	}

	
	/**
	 * 将机构数据转化成Tree格式(递归实现)
	 * @param depts
	 * @return
	 */
	public static HashMap<Object,Object> treeList(DepartmentModel model){
		/**组装本节点数据**/
		HashMap<Object, Object> map = new HashMap<Object, Object>();
		map.put("dept_no", model.get("DEPT_NO"));
		map.put("dept_name", model.get("DEPT_NAME"));
		map.put("dept_shortName", model.get("DEPT_SHORTNAME"));
		/**查询本节点下面的子节点**/
		String query_sql = "SELECT * FROM SA_DEPT WHERE DEPT_PARENT=?";
		List<DepartmentModel> depts = DepartmentModel.dao.find(query_sql,model.get("DEPT_NO"));
		List<HashMap<Object,Object>> smap = new ArrayList<HashMap<Object,Object>>();
		/**判断是否存在子节点**/
		if(depts!=null && depts.size()>0){
			map.put("leaf", false);
			/**循环子节点**/
			for(DepartmentModel dept:depts){
					HashMap<Object, Object> map1 = new HashMap<Object, Object>();
					map1.put("dept_no", dept.get("DEPT_NO"));
					map1.put("dept_name", dept.get("DEPT_NAME"));
					map1.put("dept_shortName", dept.get("DEPT_SHORTNAME"));
					List<DepartmentModel> deptslist = DepartmentModel.dao.find(query_sql,dept.get("DEPT_NO"));
					if(deptslist!=null && deptslist.size()>0){
						map1.put("leaf", false);
					}else{
						map1.put("leaf", true);
					}
					smap.add(map1);
					/***递归查找*/
					treeList(dept);
			}
			/**添加子节点数据**/
			map.put("children", smap);
		}else{
			map.put("leaf", true);
		}
		return map;
	}
	
	
	/**
	 * 分页列出部门
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<DepartmentModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("dept_no") != null){
				where = " AND dept_no=" + search.get("dept_no");
			}
			if (search.get("dept_name") != null){
				where = " AND dept_name LIKE '%" + search.get("dept_name") + "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}
		Page<DepartmentModel> Depts = null;
		
		String select_sql = "SELECT * ";
		String query_sql = "FROM SA_Dept " + where;
		try{
			Depts = DepartmentModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return Depts;
	}

	
	/**
	 * 增加部门信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){
		
		String no = params.get("dept_no")==null?"":params.get("dept_no").toString();
		String name = params.get("dept_name")==null?"":params.get("dept_name").toString();
		String type = params.get("dept_type")==null?"":params.get("dept_type").toString();
		
		DepartmentModel dm = DepartmentModel.dao.findById(no);
		if (dm == null){
			dm =new DepartmentModel();
			dm.set("dept_no",no);
			dm.set("dept_name", name);
			dm.set("dept_type", type);
			dm.save();
			return true;
		}
		return setError("你访问的部门信息已存在！");
	}
    
	/**
	 * 修改部门信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		
		String no = params.get("dept_no")==null?"":params.get("dept_no").toString();
		String name = params.get("dept_name")==null?"":params.get("dept_name").toString();
		String type = params.get("dept_type")==null?"":params.get("dept_type").toString();
		
		DepartmentModel dm = DepartmentModel.dao.findById(no);
		if (dm != null)
		{
			dm.set("dept_no",no);
			dm.set("dept_name", name);
			dm.set("dept_type", type);
			dm.update();
			return true;
		}
		return setError("您访问的部门信息不存在！");
	}
    
	/**
	 * 删除部门信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("dept_no")==null?"":params.get("dept_no").toString();
		DepartmentModel dm = DepartmentModel.dao.findById(no);
		if (dm != null)
		{
			dm.delete();
			return true;
		}
		return setError("您访问的部门信息不存在！");
	}

}
